/**
 * Manages the ItemEditorMemoryStack to go back to previous Items displayed in the ItemEditor
 * @author zulianp@lu.unisi.ch 
 */

package ch.unisi.inf.projects.java.uom.gui.commands;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.KeyStroke;

import ch.unisi.inf.projects.java.uom.gui.UOMImages;
import ch.unisi.inf.projects.java.uom.elements.Item;
import ch.unisi.inf.projects.java.uom.gui.ItemHTMLConverter;
import ch.unisi.inf.projects.java.uom.gui.itemeditor.ItemEditorMemoryStack;
import ch.unisi.inf.projects.java.uom.gui.itemeditor.ItemEditorMemoryStackListener;

public final class SelectPreviousItemDisplayedCommand extends IconedCommand implements ItemEditorMemoryStackListener{

	
	private static final long serialVersionUID = -1L;
	private final ItemEditorMemoryStack itemEditorMemoryStack;
	private final String standardDescription;

	public SelectPreviousItemDisplayedCommand (final ItemEditorMemoryStack itemEditorMemoryStack)
	{
		super("", "Click to display the previous Item", UOMImages.PREVIOUS_ITEM.getFileName());
		this.standardDescription = "Click to display the previous item";
		putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_LEFT , Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
		super.setEnabled(false);
		this.itemEditorMemoryStack = itemEditorMemoryStack;
	}

	public void nextItemChanged(final Item item) {
		return;
	}

	public void previousItemChanged(final Item item) {
		if(item == null){
			super.setEnabled(false);
			super.putValue(SHORT_DESCRIPTION, standardDescription);
		}
		else
		{
			super.setEnabled(true);
			putValue(SHORT_DESCRIPTION, ItemHTMLConverter.convertItem(item,false));
		}
	}

	public void actionPerformed(final ActionEvent ev) {
		itemEditorMemoryStack.selectPreviousItem();
		fireActionPerformed();
	}
	
}
